package com.ruoyi.web.controller.market.account;

import com.ruoyi.account.service.IAccountItemProductService;
import com.ruoyi.account.service.IBillInfoProductService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.CxSelect;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.db.domain.BillInfoProduct;
import com.ruoyi.market.http.model.param.EntryAccountParam;
import com.ruoyi.market.http.model.param.GenBillParam;
import com.ruoyi.market.http.service.MarketApiService;
import com.ruoyi.web.wapper.BillInfoProductWapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 产品账单信息Controller
 * 
 * @author ruoyi
 * @date 2023-02-02
 */
@Controller
@RequestMapping("/account/billProduct")
public class BillInfoProductController extends BaseController
{
    private String prefix = "account/billProduct";

    @Autowired
    private IBillInfoProductService billInfoProductService;
    @Autowired
    private MarketApiService marketApiService;
    @Autowired
    private IAccountItemProductService accountItemProductService;

    @RequiresPermissions("account:billProduct:view")
    @GetMapping()
    public String billProduct()
    {
        return prefix + "/billProduct";
    }

    /**
     * 查询产品账单信息列表
     */
    @RequiresPermissions("account:billProduct:view")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BillInfoProduct billInfoProduct)
    {
        startPage();
        List<Map<String, Object>> mapList = billInfoProductService.selectBillInfoProductListMap(billInfoProduct);
        mapList = new BillInfoProductWapper(mapList).wrap();
        return getDataTable(mapList);
    }

    /****************** 刷新的账单 *********************/
    @RequiresPermissions("account:billProduct:refreshBill")
    @GetMapping("/refreshBill")
    public String refreshBill(ModelMap modelMap)
    {
        List<CxSelect> itemList = accountItemProductService.selectProductSelectList();
        modelMap.put("itemList", itemList);
        return prefix + "/refreshBill";
    }

    /**
     * 查询产品账单信息列表
     */
    @RequiresPermissions("account:billProduct:refreshBill")
    @PostMapping("/refreshBillAction")
    @ResponseBody
    public AjaxResult refreshBillAction(GenBillParam genBillParam)
    {
        return toAjax(marketApiService.accountGenBillInfo(genBillParam));
    }
    /****************** 刷新的账单 *********************/


    /**
     * 导出产品账单信息列表
     */
    @RequiresPermissions("account:billProduct:export")
    @Log(title = "产品账单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BillInfoProduct billInfoProduct)
    {
        List<BillInfoProduct> list = billInfoProductService.selectBillInfoProductList(billInfoProduct);
        ExcelUtil<BillInfoProduct> util = new ExcelUtil<BillInfoProduct>(BillInfoProduct.class);
        return util.exportExcel(list, "产品账单信息数据");
    }

    /**
     * 新增产品账单信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存产品账单信息
     */
    @RequiresPermissions("account:billProduct:add")
    @Log(title = "产品账单信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BillInfoProduct billInfoProduct)
    {
        return toAjax(billInfoProductService.insertBillInfoProduct(billInfoProduct));
    }

    /**
     * 修改产品账单信息
     */
    @RequiresPermissions("account:billProduct:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        BillInfoProduct billInfoProduct = billInfoProductService.selectBillInfoProductById(id);
        mmap.put("billInfoProduct", billInfoProduct);
        return prefix + "/edit";
    }

    /**
     * 修改保存产品账单信息
     */
    @RequiresPermissions("account:billProduct:edit")
    @Log(title = "产品账单信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BillInfoProduct billInfoProduct)
    {
        return toAjax(billInfoProductService.updateBillInfoProduct(billInfoProduct));
    }

    /**
     * 删除产品账单信息
     */
    @RequiresPermissions("account:billProduct:remove")
    @Log(title = "产品账单信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(billInfoProductService.deleteBillInfoProductByIds(ids));
    }

    /**
     * 申请入账
     * @param recordId
     * @return
     */
    @PostMapping("/applyEnter")
    @ResponseBody
    public AjaxResult entryAccount(@RequestParam("recordId") String recordId)
    {
        EntryAccountParam entryAccountParam = new EntryAccountParam();
        entryAccountParam.setRecordId(recordId);
        entryAccountParam.setRecordType("PROD");

        SysUser sysUser = getSysUser();
        entryAccountParam.setOperatorId(String.valueOf(sysUser.getUserId()));
        entryAccountParam.setOperatorName(sysUser.getUserName());

        return toAjax(marketApiService.xhApplyEnter(entryAccountParam));
    }

    /**
     * 修改保存产品账户信息
     */
    @PostMapping("/refreshAccountInfo/{id}")
    @ResponseBody
    public AjaxResult refreshAccountInfo(@PathVariable("id") String id)
    {
        return toAjax(billInfoProductService.refreshAccountInfo(id));
    }

}
